package hander

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"user/api/request"
	"user/consts"
	"user/internal/dao"
	"user/internal/model"
)

func AddRoom(c *gin.Context) {
	var req request.AddRoomReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "验证失败",
			"code": 500,
			"data": err.Error(),
		})
		return
	}
	room := model.Room{
		UserId:     c.GetUint("userId"),
		Name:       req.Name,
		Sex:        req.Sex,
		Start:      req.Start,
		End:        req.End,
		Status:     consts.STATUS,
		Num:        req.Num,
		GiftNum:    req.GiftNum,
		ContentNum: req.ContentNum,
	}
	dao.Create(&room)
	c.JSON(http.StatusOK, gin.H{
		"msg":  "OK",
		"code": 200,
		"data": "AddRoom",
	})
}

func UpdateRoom(c *gin.Context) {
	var req request.UpdateRoomReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "验证失败",
			"code": 500,
			"data": err.Error(),
		})
		return
	}
	var room model.Room
	dao.GetOneById(req.Id, &room)
	if room.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "直播间不存在",
			"code": 500,
			"data": nil,
		})
		return
	}
	rm := model.Room{
		Id:     room.Id,
		Status: consts.STATUS,
	}
	dao.Update(&rm)
	c.JSON(http.StatusOK, gin.H{
		"msg":  "OK",
		"code": 200,
		"data": "AddRoom",
	})
}

func AddFan(c *gin.Context) {
	var req request.AddFanReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "验证失败",
			"code": 500,
			"data": err.Error(),
		})
		return
	}
	fan := model.Fan{
		UserId: c.GetUint("userId"),
		Fans:   req.Fans,
	}
	dao.Create(&fan)
	var room model.Room
	ro := model.Room{
		UserId: c.GetUint("userId"),
		Num:    room.Num + req.Fans,
	}
	dao.Update(&ro)
	c.JSON(http.StatusOK, gin.H{
		"msg":  "OK",
		"code": 200,
		"data": "AddRoom",
	})
}

func ListRoom(c *gin.Context) {
	var room []model.Room
	dao.Find(&room)
	c.JSON(http.StatusOK, gin.H{
		"msg":  "OK",
		"code": 200,
		"data": room,
	})
}

func OutRoom(c *gin.Context) {
	ro := model.Room{
		Status: consts.STATUS,
	}
	dao.Update1(&ro)
	c.JSON(http.StatusOK, gin.H{
		"msg":  "OK",
		"code": 200,
		"data": "OutRoom",
	})
}

func AddNum(c *gin.Context) {
	var req request.AddNumReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "验证失败",
			"code": 500,
			"data": err.Error(),
		})
		return
	}
	var room model.Room
	dao.GetOneById(req.Id, &room)
	if room.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"msg":  "直播间不存在",
			"code": 500,
			"data": nil,
		})
		return
	}
	ro := model.Room{
		Status: consts.STATUS,
		Num:    room.Num + req.Num,
	}
	dao.Update1(&ro)
	c.JSON(http.StatusOK, gin.H{
		"msg":  "OK",
		"code": 200,
		"data": "AddNum",
	})
}
